Add setBuffer
method for static buffers and/or multiple displays sharing a single buffer
#149
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds a
setBuffer
method that allows users to supply their own display buffer. This means a static buffer can be used instead of a dynamic one, and that multiple displays can reuse the same buffer.Motivation
However, the new version of the library allocates one buffer for each instance. On an UNO, you need 2 KiB for two displays, so the code I had no longer works.
By adding the option to let the user decide what buffer to use, this problem is resolved, and you can use multiple OLED displays, even on AVR Arduinos with limited RAM.
Changes
I added two small methods and one member variable to the
Adafruit_SSD1306
class. OnlysetBuffer
is public. The actual amound of real code that has been added is just 3 lines and some declarations.void setBuffer(uint8_t *buffer)
: this methods just sets the pointer to the buffer, and remembers that it shouldn't callfree(buffer)
later. If a buffer was already allocated, it's deallocated first.void deallocateBuffer(void)
: this method contains the code that was previously in the destructor. If the pointer to the buffer is not null, and if the buffer was allocated by this object, it's deallocated.It is still called from the destructor, and also from the new
setBuffer
method.enum : bool { Borrowed, Allocated } bufferPolicy
: this member variable keeps track of whether the buffer was allocated by this object, or if it's borrowed from someone else.It's private, and users of the library don't have to worry about it.
I also added an example, which is just a copy of the
ssd1306_128x64_spi
example, but with a static buffer instead of a dynamic one.Finally, I provided a macro to detect whether the
setBuffer
method is available. This can be useful when different version of the library remain in use (e.g. Teensyduino comes with its own version).Known Limitations
None.
These changes should be 100% backwards compatible. If you don't use the new
setBuffer
method, nothing changes.Tests
I tested the new and the old example for an SPI 128×64 SSD1306 display on an Arduino UNO and a Teensy 3.2.
Everything worked as expected.